<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Harsha Chinni on Medium]]></title>
        <description><![CDATA[Stories by Harsha Chinni on Medium]]></description>
        <link>https://medium.com/@the-harsha-chinni?source=rss-18f16a28c6ad------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*xoqc8wr97kOtbwFWR0Hhcw.jpeg</url>
            <title>Stories by Harsha Chinni on Medium</title>
            <link>https://medium.com/@the-harsha-chinni?source=rss-18f16a28c6ad------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 14:47:34 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@the-harsha-chinni/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Virtual scrolling in a mat-select dropdown using a nested form group— Angular 11]]></title>
            <link>https://the-harsha-chinni.medium.com/virtual-scrolling-in-a-mat-select-dropdown-using-a-nested-form-group-angular-11-6c770bd3cd56?source=rss-18f16a28c6ad------2</link>
            <guid isPermaLink="false">https://medium.com/p/6c770bd3cd56</guid>
            <dc:creator><![CDATA[Harsha Chinni]]></dc:creator>
            <pubDate>Wed, 10 Feb 2021 06:09:41 GMT</pubDate>
            <atom:updated>2021-02-10T06:14:49.208Z</atom:updated>
            <content:encoded><![CDATA[<p>It’s been a long time I have written an article. Can’t wait for you to read this one.</p><p>Okay! What and why’s of Virtual Scrolling in Angular it is!! Let’s get started</p><p><strong>What is virtual scrolling?</strong></p><p>Loading only the required amount of data to display to the end-user at a given point of time</p><h3>Why do we need Virtual Scrolling?</h3><p>In real-world applications, there are scenarios where we need to load a huge data set of values to a select dropdown/div blocks from which the users can select a value(s).</p><p>Let’s suppose we have to load 10k records into a select dropdown from which the user can select a value(s). From the browser’s standpoint, all the 10k records are fed to the select drop-down at once, implies the time to taken to render the entire data set to the view is very high. This will lead to a non-responsive browser screen.</p><p>This is where the concept of Virtual Scrolling will come to our rescue. Instead of loading the entire data set into the browser at once, leading to long render times, Virtual Scrolling will only load the part of data only the user can see. We have to write code to calculate the total length of the viewport of the drop-down; length to display a record in the drop-down and finally the no. of records to be displayed at a single point of time when the drop-down is opened.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*dktEgO6jryNKT2G9" /></figure><p>Therefore, the time taken by the browser to render these 4 records or any 4 records when the user scrolls down is very less compared to render all the 10k records at once. To notice the difference, we can profile the application.</p><h4>Alright, I get it, enough of theory and let’s dive deep into the code 🌊</h4><h3><strong>What do we need??</strong></h3><p>Let’s set up a mock Angular project with Angular Material, there are many articles on Medium and in Google which explains this very clearly. Therefore I am not going to explain this step. I have also discussed the same in my previous article — <a href="https://medium.com/@the_harsha_chinni/how-to-build-a-dynamic-table-component-in-angular-9-9aacc3d81b08">How to build dynamic Mat-table components in Angular</a></p><p>Once we have the project in place with Angular Material installed, let’s create a view in the app.component.html which renders a user registration form.</p><p>In this project, I have decided to use the Reactive Forms in combination with Angular Material form fields to build the ‘User Registration’ form to replicate close to the enterprise applications. We have a ‘user form’ which has a nested form group ‘address’ which has one of the form-controls as ‘zipCode’.</p><p>At this point, I have decided to create 10,000 records of 4 digits each ranging from 0000–9999 and feed to the mat-select ZipCode dropdown to recreate the page lag involved to render this huge data set in the Browser.</p><p>Note — The initial value of the zip code is set to ‘1000’</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iCMiEkeWDt775ZrGkCnLRg.png" /><figcaption>app.component.ts</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PdBjyivWnT9337LiJjs-Tw.png" /><figcaption>The bottom part of the app.component.html code</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cNZWlMXq7rtRRGXl2G9eAw.png" /><figcaption>User Registration Form view</figcaption></figure><p>If you’re a curious soul like me, see the performance report generated using the Profiler in the browser. Let’s profile the application and see the time taken to render the data into the zip codes drop-down. Right-click on your application tab, click on the inspect option, go to the performance tab, click on the record icon to start recording, click on the target select drop-down, scroll down the drop-down. Now, stop recording in the performance tab, from the charts produced during this process, look for the render time during the click on the select drop-down action. This should give you the statistics of the time taken by the browser to directly render all the 10k records into the select drop-down.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NEXtYpL3qdcHHUrI6ROCmQ.gif" /><figcaption>Profiling the zip code select dropdown.gif</figcaption></figure><p>At the end of the GIF, notice the time taken to render this data by the browser — <strong>2766 ms</strong>. This will create a bad experience for the end-users using the application.</p><p>Also, think about what is the probability of a user scrolling all the way down to the end of the list? This is where we have to be smart and load only the portion data that is being viewed by the end-user. Yes, we have a name for this solution — <strong>Virtual Scrolling</strong>; load only the portion of the data based on the user actions.</p><p>Well, there are working examples available right now regarding the same topic. Why am I writing this article and what interested me to write this article in the first place? The combination of mat-select drop-down in a form group; and the ability to set an initial value when the page reloads were not so evident in the first place. The Angular Material documentation does not have an example of the Virtual Scrolling with MatSelect and it was tough to find good examples where I can render the select drop-down with an initial value set.</p><p>Let’s see the <strong>&lt;cdk-virtual-scroll-viewport&gt; </strong>component in action<strong>. </strong>Using this component to build the view, will significantly reduce the page lag as the rendering time is pretty low.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hOiNmh2BQf0LgUvC39m9OA.png" /><figcaption>with cdk-virtual-scroll-viewport</figcaption></figure><p>Let’s follow the above steps mentioned to profile the application again to notice a significant performance improvement while rendering the page.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xulGivI9bueQ-2vdwPxXZg.gif" /><figcaption>Profiling the page with Virtual scrolling in place</figcaption></figure><p>The time taken to render the page, in this case, is 78ms. Since we developers are performance enthusiasts, let’s calculate the overall improvement — (2776/ 78; render time w/o virtual scroll / render time with virtual scroll) by <strong>35 times.</strong></p><p>The challenge is, the documentation is not straight forward, and we have to solve a few puzzles to get the required functionality.</p><p>The first catch is we have to provide the &lt;cdk-virtual-scroll-viewport&gt; with a class (“<strong>virtual-scroll</strong>”), which sets the height of the drop-down. Otherwise, we will not be able to view the records in the drop-down</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/394/1*zuo4gx3j4SzrC8pp4sPmJw.png" /><figcaption>app.component.css</figcaption></figure><p>Now, we will be able to see the drop-down displaying the records and there will not be any lag. But there is a problem!! If you notice we are not able to see the initial value of ‘1000’ not being displayed when the page is refreshed.</p><p>Here comes our second catch, as only a portion of data is loaded to the view, in this case, only 5 values are displayed at a time if the initial value set to the select dropdown is not created in the view, i.e., if the &lt;mat-option&gt; with the value of ‘1000’ (initial value set in the TS file) is not created in the view, the select drop-down will not be able to set the initial value and we would not be able to see. To overcome this issue, we have to conditionally create the mat-option with the selected value of the zip code if any.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*k_hD5XXFbIuKCg1kt_WaNA.png" /></figure><p>That’s it we’re done 😄.</p><p>Thank you so much for going through my work, hope it will help you resolve the challenge(s). You have a good one!</p><h3>Resources:</h3><ol><li>You can find the code in — <a href="https://github.com/HarshaChinni/virtual-scroll-angular/tree/master">https://github.com/HarshaChinni/virtual-scroll-angular/tree/master</a></li><li><a href="https://angular.io/guide/reactive-forms#creating-nested-form-groups">https://angular.io/guide/reactive-forms#creating-nested-form-groups</a></li><li><a href="https://material.angular.io/cdk/scrolling/api">https://material.angular.io/cdk/scrolling/api</a></li><li>Video recording for GIFs — Quick time player (Macbook Pro)</li><li>For code snippets — <a href="https://carbon.now.sh/">https://carbon.now.sh/</a> and VS code editor</li><li>Lucid Chart</li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6c770bd3cd56" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to build a dynamic table component in Angular 9?]]></title>
            <link>https://the-harsha-chinni.medium.com/how-to-build-a-dynamic-table-component-in-angular-9-9aacc3d81b08?source=rss-18f16a28c6ad------2</link>
            <guid isPermaLink="false">https://medium.com/p/9aacc3d81b08</guid>
            <category><![CDATA[angular-material]]></category>
            <category><![CDATA[angular]]></category>
            <dc:creator><![CDATA[Harsha Chinni]]></dc:creator>
            <pubDate>Mon, 16 Mar 2020 01:41:36 GMT</pubDate>
            <atom:updated>2020-03-16T07:25:51.464Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/728/1*pMLKMTj69QFNeeD9aOmFwA.jpeg" /></figure><p>If you are working on an Angular project, there will be several pages where data is represented using a table structure.</p><p>One of the libraries we can leverage is the Angular material library (material.angular.io). The material library’s table component will not only make our lives easy to build a table with the given data but also seamlessly integrate basic functionalities such as Pagination, Sorting and Search operations 😍.</p><p>To be in line with the DRY (<strong>Don’t Repeat Your Code</strong>) principle, let us develop a dynamic mat-table component and reuse it where ever necessary.</p><p>Without further ado, let’s jump into the code to build a dynamic material table component that can be used in components across the project 🙈.</p><p>First of all, create an Angular project using the ng new &lt;project_name&gt; command. The latest stable version of the angular is V9.0.6</p><p>Secondly, install the angular-material library by running the command npm install --save @angular/material in the project directory. The latest version of angular-material is V9.1.2</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LdTbL5fU4rWGgjW1J9hG6A.png" /><figcaption>package.json</figcaption></figure><p>Let us create a table component using the command ng g c mat-table which will update the app.module.ts component’s declarations to accommodate the mat-table component.</p><p><strong>FYI: </strong>In the above command ng g c &lt;component_name&gt;; g is short for generate and c is short for component</p><p>At this point, we should go ahead to import the MatTableModule into the app.module.ts. The following code will have both the table module and component imported into the project. If you see an error ‘<strong>Can’t find the module </strong><strong>@angular/cdk/table</strong>&quot; go ahead and install the @anglular/cdk using the command npm i @angular/cdk.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ue3ok6jxzrMSZsW15o56sw.png" /><figcaption>AppModule.ts</figcaption></figure><p>Go ahead and run the project using ng serve -o to start the project in a new tab if you have not already started.</p><p><strong>Note</strong>: If you are using angular V8.x in your project, don’t worry if you come across an error: <strong>“error TS1086: An accessor cannot be declared in an ambient context.”</strong>. When you have installed the angular/material, the CLI would have fetched the latest <strong>V 9.X</strong> from the npm repository. To resolve this error, make sure that your @angular/cli and @angular/core versions are inline with @angular/material version by using ng update @angular/core @angular/cli command. You can check package.json whether core, CLI and material lib versions are in ^9.0.0 version.</p><p>The above will integrate the material lib setup into our project.</p><p>At this point, navigate to the mat-table component to develop the basic table structure. We can come with mock-data that can be used to display in the table. Also, initialize the mock-data using new MatTableDataSource() constructor, we will discuss the reason down the line.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KP3g7slxl0NNHeUFNOe4-g.png" /></figure><p>Let us build the HTML side of the table to render the data. The following code will do the job for now:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ER5j0c9iy0xxpwMAlyLtHA.png" /></figure><p>Render the mat-table component in the app.component.ts using the selector tag &lt;app-mat-table&gt;&lt;/app-mat-table&gt; to display the table</p><p>The following is the output that will be displayed in the browser at the localhost:4200 :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ORn0FD3gD-ZfpXhAjmJcYg.png" /><figcaption>Browser output</figcaption></figure><p>At this point, if we have to retrieve a table with 15–25 columns, we will have to repeat the &lt;ng-container&gt;&lt;/ng-container&gt; piece of code in mat-table. component.html for as many columns. It will be difficult to maintain the HTML code in the long run.</p><p>We can overcome this issue by automating the &lt;ng-container&gt; piece of code to render as many columns as we use in the tableData. To achieve this, go-ahead to the mat-table.component.html and make the following changes:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*V-wrE-majlCET9L_EIN_jQ.png" /></figure><p>From the above code, we have looped through a list of tableCols to assign the matColumnDef property in the th tag. We have used the column name key to fetch the respective value from the matCellDef’s profile object which in this case it the row data.</p><p><strong>Note</strong>: titlecase is a built-in pipe provided by Angular to make the first letter of a word capital letter</p><p>Can you think of the number of lines of HTML code the above code would save if we have 7–15 columns to display, which is the reality of 60% of the cases in enterprise apps? One more thing to keep in mind is, if we have to add a new column, we can simply add that to each record in the tableData property in the mat-table.component.ts and update the tableCols with the new column name. That is it, the new data will be rendered in the table without adding &lt;ng-container&gt;...&lt;/ng-container&gt; a piece of code for every new column in the HTML scheme of things.</p><p>At this point, I hope you are on the same page as I am. Feel free code along for a better experience.</p><p>The reason to use <strong>MatTableDataSource</strong> is, it’s easier to integrate sorting, pagination and searching to the table. Else we need to perform these operations manually by defining the respective functions to modify the data and update the view accordingly.</p><p>Let’s go ahead and add Sorting functionality to the table component. Inside the mat-table.component.ts let’s add the following line of code:</p><p>@ViewChild(MatSort, {static: true}) sort: MatSort; I hope you are aware of the ViewChild decorator from Angular; if not please refer to <a href="https://angular.io/api/core/ViewChild">https://angular.io/api/core/ViewChild</a></p><p>Hey, I want you to pause and think here for a moment. Are we missing anything 🤔? Hmm….Wait what? Hell yeah, how can I not add MatSortModule from @angular/material/sort to the app.module.ts to make the sort directive work?😎</p><p>Furthermore, assigning the tableDateSrc’s sort to above declared ViewChild sort property as follow:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*r8z9je8RJIVmjO2rNJ-avw.png" /><figcaption>Updated mat-table.component.ts for sorting</figcaption></figure><p>Also, we have to update the code in html by adding matSort directive to the &lt;table&gt; tag and mat-sort-header to the th tag as follows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6uoxBUZI3qJTsLar8UW2TA.png" /><figcaption>mat-table.component.html with sorting</figcaption></figure><p>Also, keep in mind, if we have not automated the table’s &lt;ng-container&gt; part of code, we have to update every column with the <strong>mat-sort-header</strong> directive.</p><p>At this point of time, go ahead and play around with the table headers in the browser. You may notice an error in the browser console, it may be complaining about the arrow animations. To overcome this error import the BrowserAnimationsModule in the app.module.ts</p><p>If you remember the roadmap, the next task is to integrate the Pagination functionality to the table. Let’s go get that working 😃</p><p>For this task, we need to add &lt;mat-paginator&gt; tag to the table HTML as follows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AhbZrf4FAY_oGBejTeu4Lg.png" /><figcaption>Update mat-table.component.html for Pagination</figcaption></figure><p>Similar to updating the sort property in tableDataSrc, we need to update the paginator property to integrate Pagination. The following will help understand the same:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7B-MjTTV3sl2Ws-8QSDIVg.png" /><figcaption>Update mat-table.component.ts for Pagination</figcaption></figure><p>I know you’ll be thinking about whether there is a MatPaginatorModule . Take a bow, you’re on the right track. Go ahead and update the app.module.ts</p><p>Now, let’s integrate Search functionality to the mat-table:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5sbySdTH1xfu690IbXGugg.png" /><figcaption>Updated mat-table.component.html with search functionality</figcaption></figure><p>mat-table.component.ts is updated with the onSearchInput() fn as follows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*r9r-Zo9oJVTcxWjKu9nAyQ.png" /><figcaption>Updated mat-table.component.ts with search functionality</figcaption></figure><p>To make the table data and table column names dynamic, update the mat-table.component.ts code as to below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*G3-GE7-0kNX_ReXmYKAL_w.png" /><figcaption>Update the tableData and tabCols to be dynamic inputs using @Input()</figcaption></figure><p>If you observe the above code, we are no longer getting the data from the mat-table.component.ts rather we are feeding data from the app.component.ts</p><p>Have a look at the app.component.ts/html code:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/958/1*WilsGu3hONCbrlse-JpjSA.png" /><figcaption>app.component.ts with the table data</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iHL-xFTEQchaJPYzezvfNw.png" /><figcaption>Look at the input tags tableData and tableColumns inputs</figcaption></figure><p>Boom!!👻 we have completed coding the dynamic mat-table component with Sort, Pagination and Search functionality.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BMjeFn_EnCSYt5OIhQWeCg.png" /><figcaption>Final output in the browser after integrating styles and font</figcaption></figure><p>If we place one more &lt;mat-table&gt; selector in the app.component.html with a different data fed to the mat-table we see the following rendered in the browser:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ug9MKWX2uKeQQyxnKeCLHw.png" /><figcaption>Browser rendering different data with the same mat-table component</figcaption></figure><p>The following is the code to render the above table:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/964/1*GzOa3pw0maeein48PsNytw.png" /><figcaption>app-component.html</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mBR8f4lFNIZ5TrhdPhsK1g.png" /><figcaption>second table data in the app-component.ts</figcaption></figure><p>From the above code, we are sure that irrespective of the number of columns and data fed to the mat-table component, it can render the table in the browser. We can use the same analogy in different components across the project.</p><p>I know this is a lengthy article 😔. Let’s continue in the next article to enhance the table code to dynamically provide options to have checkboxes to select a few records to perform actions 😃. Moreover, we will also integrate the ability to have a dynamic button for each row to view record specific details using angular routing.</p><p>Thanks for your time, I hope you have learnt something new and useful. Until then take care, have a good rest of your day 🙃</p><p>You can find the code in my GitHub: <a href="https://github.com/HarshaChinni/ng-dynamic-material-table">https://github.com/HarshaChinni/ng-dynamic-material-table</a></p><p><strong>FYI</strong>: You need to import Roboto font and material themes for material-library styles to work properly. Please go through the index.html and styles.css files to understand further.</p><p><strong>References:</strong></p><ol><li><a href="https://angular.io/docs">https://angular.io/docs</a></li><li><a href="https://material.angular.io/components/table/overview#getting-started">https://material.angular.io/components/table/overview#getting-started</a></li><li><a href="https://material.angular.io/guide/getting-started">https://material.angular.io/guide/getting-started</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9aacc3d81b08" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[What am I experiencing with my Full-Stack development journey?]]></title>
            <link>https://the-harsha-chinni.medium.com/what-am-i-experiencing-with-my-full-stack-development-journey-706780bb2cd6?source=rss-18f16a28c6ad------2</link>
            <guid isPermaLink="false">https://medium.com/p/706780bb2cd6</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[flask-framework]]></category>
            <dc:creator><![CDATA[Harsha Chinni]]></dc:creator>
            <pubDate>Sat, 29 Feb 2020 20:39:45 GMT</pubDate>
            <atom:updated>2020-02-29T21:17:24.491Z</atom:updated>
            <content:encoded><![CDATA[<p>#Flask #Python #Fullstack</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/870/1*5INViibAjl_5Da5ECtzo2Q.jpeg" /></figure><p>Being a FullStack developer is a wonderful experience as I have to juggle between the front-end and back-end tasks simultaneously. With Angular 9 in the front-end and Flask framework in the back-end along with the Oracle DB, I am architecting and developing a project.</p><p>Recently, I have encountered a bug from the server-side Flask API (A micro-framework of Python) which falls in lines of — “typeerror parse() got an unexpected keyword argument ‘locations’ in flask-apispec wrapper.py line 46.”</p><p>I started debugging the code concerning server logs stack-trace. I have peeked the wrapper.py file in the flask-apispec library package. I have been through the code to understand what it is trying to achieve.</p><p>It took me and a couple of other developers, a couple of days to figure out the root cause of the issue — The issue was originating because of the @use_kwargs(LoginSchema()) decorator</p><p>We had to follow a trial-and-error approach to rule out the contention of other packages/libraries/code-developed. The following options were identified as potential causes initially</p><ol><li>Docker env</li><li>Latest commits into the develop branch</li><li>Current code in dev etc</li><li>Flask_apispec version (bump-up/down)</li></ol><p>Let us create an ‘authentication’ mock project to have a better understanding of the code context. The folder structure to organize the code goes as follows:</p><p><strong>authentication-project /</strong></p><p><strong>args/ Login.py</strong></p><p><strong>endpoint/ Login.py</strong></p><p><strong>impl/ Login.py</strong></p><p><strong>main.py</strong></p><p>The main.py file — Hosts the flask instance of the app and all other packages (Flask_ApiSpec etc) we register our app against</p><p>The ‘args’ (arguments) folder hosts the Schemas for our endpoints</p><p>The ‘endpoint’ folder hosts the code for the endpoint-view function</p><p>The ‘impl’ (implementation) folder hosts the code for the implementation of biz logic for a particular endpoint</p><p>Create a Flask app -</p><ol><li>Import the Flask class from the flask package</li><li>Import the FlaskApiSpec from the flask_apispec package</li><li>Register the flask app against app variable against the Flask_apispec</li><li>Register the login_blueprint variable in the FlaskApiSpec object to help create the swagger documentation (Open API spec)</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*e5sBrXRMG2BnIAi5" /></figure><p>In the below logic, we create a schema for the Login request</p><ol><li>Imports the base ‘Schema’ class from the marshmallow package and extend our LoginSchema class against the Schema class</li><li>Imports the fields and Validation class to perform the necessary operations to register the fields and validate them respectively</li><li>The Validation fn gives us the capability to perform min and max length check operations</li><li>Required = True, makes a field necessary. If the field is not provided in the request body, the marshmallow gives the client 422 HTTP code which stands for an unprocessable entity. It will not allow the request to enter the actual logic</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*gLhEKeyyQ42DFzjL" /><figcaption>args/login.py</figcaption></figure><p>To register a route to a particular endpoint, we develop the following logic in the endpoints folder’s login.py file as follows:</p><ol><li>Import the Blueprint from flask, to register all the endpoints of an app against it</li><li>Import use_kwargs, to read the request.body params of the POST request (‘/login’)</li><li>Register a blueprint named ‘login’</li><li>Decorate the endpoints with @doc(type=[‘endpoint-type’]) to generate the swagger documentation at location <a href="http://localhost:5000//swagger">http://localhost:5000//swagger</a> or <a href="http://localhost:5000/swagger-ui">http://localhost:5000/swagger-ui</a></li><li>Import the verify_login_credentials method from the /impl folder to perform the verification logic</li></ol><p>From the Flask-ApiSpec documentation, it is clear that the: “<strong>flask-apispec</strong> automatically generates Swagger 2.0 documentation for view functions and classes using <a href="https://apispec.readthedocs.io/">apispec</a>.:”</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PGRMBGm1WxiEGK0pflVe6w.png" /></figure><p>Take a look at the verify_login_credentials logic:</p><ol><li>We have the actual business logic in this file ‘login.py’ under the impl (implementation) folder</li><li>In this case, we need to query the users table to get the username and password details from the DB using the SqlAlchemy ORM</li><li>In the interest of time, I am not going to write the actual logic to perform a DB query</li><li>Assuming that the end-user has provided the valid username and password; the verify_login_credentials returns True</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*MyU7XOiWJwoMu_Sv" /><figcaption>impl/login.py</figcaption></figure><p>Coming to the error that I have encountered from the @use_kwargs(LoginSchmea()) in the endpoints/login.py file — after hours of debugging, we figured out that the issue was coming from one of the <strong>child package dependencies </strong>of the flask_apispec package called ‘webargs’</p><p>On Feb-27–2019, webargs released their latest V6.0.0 which was a major release from the previous stable V5.5.3</p><p>FYI, the below is the changelog history of the webargs package from PyPI</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*w5cb1GdlRBIJD3GY" /></figure><p>In the flask_apispec meta, the package versions we mentioned as <strong>webargs&gt;=5.5.3</strong>, which led to the installation of 6.0.0 version</p><p>To resolve the issue, we had to bump-down the version of webargs to 5.5.3 using <strong>webargs==5.53 </strong>in the requirements.txt file to command Docker to install the exact version</p><p>As the context to find the root-cause was limited, I had to dig through different parts of code and try out different ways to get a better context and eliminate other possibilities.</p><p>I wanted to share this article along with the code context for a better understanding of the issue at hand. Moreover, there are no StackOverflow questions for this issue yet as it is a relatively new one.</p><p>I want to conclude by saying that an error is not always because of the code you write, it can be because of any component in the project. In this case, it is because of metadata of a third-party library.</p><p>I hope this article will save some precious dev time and rescue you from the situation I was in.</p><p>Thanks for your time to read the complete article. Hope you have a great time ahead</p><p>References:</p><ol><li><a href="https://marshmallow.readthedocs.io/en/stable/quickstart.html#specifying-defaults">https://marshmallow.readthedocs.io/en/stable/quickstart.html#specifying-defaults</a></li><li><a href="http://exploreflask.com/en/latest/blueprints.html">http://exploreflask.com/en/latest/blueprints.html</a></li><li><a href="https://flask-apispec.readthedocs.io/en/latest/usage.html">https://flask-apispec.readthedocs.io/en/latest/usage.html</a></li><li><a href="https://pypi.org/project/flask-apispec/0.8.3/">https://pypi.org/project/flask-apispec/0.8.3/</a></li><li><a href="https://webargs.readthedocs.io/en/latest/changelog.html">https://webargs.readthedocs.io/en/latest/changelog.html</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=706780bb2cd6" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>